//
//  BaseViewController.m
//  娱
//
//  Created by dllo on 15/9/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<MBProgressHUDDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) MBProgressHUD *HUD;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 / 255.0 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    // 调整导航栏字的颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

    self.navigationController.delegate = self;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HUDHidden:) name:@"HUDHidden" object:nil];
    
    
    self.array = [NSMutableArray array];
    for (NSInteger i = 1; i < 5; i++) {
       UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.tiff",i]];
        [_array addObject:image];
    }
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(- 20, -20, 140, 140)];
    self.imageView.animationImages = _array;
    self.imageView.animationDuration = 1;
    self.imageView.animationRepeatCount = 0;
    _imageView.layer.cornerRadius = 10;
    _imageView.clipsToBounds = YES;
    [_imageView startAnimating];
    
    
    
}

- (void)showHUD:(BOOL)hidden
{
    [self.HUD removeFromSuperview];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    self.HUD.mode = MBProgressHUDModeCustomView;
    
    [self.view addSubview:_HUD];
    
    
    self.HUD.customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    

    [self.HUD.customView addSubview:_imageView];
    
    
//    NSLog(@"%f",self.HUD.progress);
    
    _HUD.delegate = self;
    
    _HUD.color = [UIColor blackColor];
    
//    _HUD.labelText = @"加载中...";
//    
//    _HUD.detailsLabelText = @"网络加载中 ,请稍等";
    
    _HUD.dimBackground = YES;
    
    _HUD.hidden = hidden;
 
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HUDHidden:) name:@"HUDHidden" object:nil];
    //        static dispatch_once_t onceToken;
    //        dispatch_once(&onceToken, ^{
    //                       });
    
    

}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    
    if (operation == UINavigationControllerOperationPop) {
//        NSLog(@"POP");
        self.HUD.hidden = YES;
        [self.HUD removeFromSuperview];
    }
    return nil;
}

- (void)HUDHidden:(NSNotification *)noti
{
    
    [self.view bringSubviewToFront:_HUD];
    if ([noti.object isEqualToString:@"1"]) {
//        NSLog(@"YES");
        self.HUD.hidden = YES;
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [self.HUD removeFromSuperview];
    }else{
//        NSLog(@"NO");
        [self showHUD:NO];
    }
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
